﻿using UnityEngine;
using UnityEngine.UI;

namespace UnityView
{
    public class UIView : UILayout
    {
        public CanvasRenderer CanvasRenderer { get; set; }

        protected Image imageComponent;
        public Image ImageComponent
        {
            protected set { imageComponent = value; } 
            get { return imageComponent ?? (imageComponent = UIObject.AddComponent<Image>()); }
        }

        protected Color InternalBackgroundColor
        {
            set
            {
                if (ImageComponent == null) return;
                if (UIAnimation.OnAnimate())
                {
                    UIAnimation.Append(new ImageColorAction(ImageComponent, ImageComponent.color, value));
                }
                else
                {
                    ImageComponent.color = value;
                }
            }
            get
            {
                return ImageComponent == null ? Color.clear : ImageComponent.color;
            }
        }
        
        [Animatable]
        public virtual Color BackgroundColor
        {
            set { InternalBackgroundColor = value; }
            get { return InternalBackgroundColor; }
        }

        [Animatable]
        public float Alpha
        {
            set
            {
                if (ImageComponent == null) return;
                if (UIAnimation.OnAnimate())
                {
                    UIAnimation.Append(new AlphaAction(this, ImageComponent.color.a, value));
                }
                else
                {
                    Color c = ImageComponent.color;
                    c.a = value;
                    ImageComponent.color = c;
                }
            }
            get
            {
                if (ImageComponent == null) return 0;
                return ImageComponent.color.a;
            }
        }

        protected Outline OutlineComponent;

        public UIBorder Border
        {
            get
            {
                return new UIBorder(OutlineComponent.effectColor, OutlineComponent.effectDistance, OutlineComponent.useGraphicAlpha);
            }
            set
            {
                if (OutlineComponent == null) OutlineComponent = UIObject.AddComponent<Outline>();
                OutlineComponent.effectColor = value.Color;
                OutlineComponent.effectDistance = value.Offset;
                OutlineComponent.useGraphicAlpha = value.UseGraphicAlpha;
            }
        }

        public virtual Sprite BackgroundSprite
        {
            set => ImageComponent.sprite = value;
            get => ImageComponent.sprite;
        }

        public bool IsRaycastTarget
        {
            get => ImageComponent.raycastTarget;
            set => ImageComponent.raycastTarget = value;
        }

        private const string DefaultUIViewName = "UI View";
        
        /// <summary>
        /// 构造方法
        /// 不传参时，默认以 Canvas 为父物体
        /// 传参时，以参数为父物体
        /// 也可根据传入的 gameObject 构造对象
        /// </summary>
        public UIView(string name = DefaultUIViewName) : this(UICanvas.GetInstance(), name) { }

        public UIView(RectTransform transform, string name = DefaultUIViewName) : base(transform)
        {
            CanvasRenderer = UIObject.AddComponent<CanvasRenderer>();
        }

        public UIView(IViewLayout parent, string name = DefaultUIViewName) : base(parent, name)
        {
            CanvasRenderer = UIObject.AddComponent<CanvasRenderer>();
        }

        public UIView(GameObject gameObject) : base(gameObject)
        {
            CanvasRenderer = gameObject.GetComponent<CanvasRenderer>() ?? gameObject.AddComponent<CanvasRenderer>();
        }

        public static GameObject BaseView(string name = DefaultUIViewName)
        {
            var gameObject = new UILayout(name).UIObject;
            gameObject.AddComponent<CanvasRenderer>();
            return gameObject;
        }
    }
}
